﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;

namespace displaycongress_desktop
{
    public partial class SessionScreen : Form
    {
        Process processInstance;
        public delegate void DelLoadURLViewer(String url);
        public DelLoadURLViewer loadURLViewer;

        public SessionScreen()
        {
            InitializeComponent();
            loadURLViewer = new DelLoadURLViewer(LoadURLViewer);
        }

        public void LoadFileViewer(String file)
        {
            try
            {
                processInstance = Process.Start(Directory.GetCurrentDirectory() + "\\FreeFileViewer\\FreeFileViewer.exe", file);

                while (Process.GetProcessesByName(processInstance.ProcessName)[0].MainWindowTitle.Equals(""))
                {
                    Thread.Sleep(100);
                }

                SetParent(processInstance.MainWindowHandle, panel1.Handle);
                ShowWindow(processInstance.MainWindowHandle, 3);
            }
            catch (Win32Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }
            catch (InvalidOperationException ex2)
            {
                Console.WriteLine("Exception: " + ex2.Message);
            }
        }

        public void LoadURLViewer(String url)
        {
            try
            {
                Console.WriteLine("browser opstarten!");

                processInstance = Process.Start("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", url);
                
                Thread.Sleep(2000);
                
                SetParent(processInstance.MainWindowHandle, panel1.Handle);
                ShowWindow(processInstance.MainWindowHandle, 3);
            }
            catch (Win32Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }
            catch (InvalidOperationException ex2)
            {
                Console.WriteLine("Exception: " + ex2.Message);
            }
        }

        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        private void SessionScreen_FormClosing(object sender, FormClosingEventArgs e)
        {
            Console.WriteLine("Closing!");
            try
            {

                Process[] proc = Process.GetProcessesByName("FreeFileViewer");
                foreach (Process prs in proc)
                {
                    prs.Kill();
                }
                // Kill the FreeFileViewer for now.
                //processInstance.Kill();

                e.Cancel = true;
                Engine.showMainWindow();
            }
            catch (NullReferenceException ex)
            {
                Console.WriteLine("BOEM!");
            }
        }

        public void SetIpLabel(string ipaddr)
        {
            ipaddr_label.Text = "Connect to: " + ipaddr;
        }
    }
}